/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * 日期格式化
 */
export function Format(date, fmt) {
  if (fmt === undefined) {
    fmt = 'yyyy-MM-dd hh:mm:ss'
  }
  var o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds() // 秒
  }
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  }
  return fmt
}
/**
 * 获取昨天riqi
 */
export function getDay(num, str) {
  var today = new Date()
  var nowTime = today.getTime()
  var ms = 24 * 3600 * 1000 * num
  today.setTime(parseInt(nowTime + ms))
  var oYear = today.getFullYear()
  var oMoth = (today.getMonth() + 1).toString()
  if (oMoth.length <= 1) oMoth = '0' + oMoth
  var oDay = today.getDate().toString()
  if (oDay.length <= 1) oDay = '0' + oDay
  return oYear + str + oMoth + str + oDay
}

/**
 * 获取昨天
 */
export function Foryest() {
  var today = new Date()
  var yesterday = new Date(today.setTime(today.getTime() - 3600 * 1000 * 24 * 1))
  var y = yesterday.getFullYear()
  var m = yesterday.getMonth() + 1
  var d = yesterday.getDate()
  if (m < 10) {
    m = '0' + m
  }
  if (d < 10) {
    d = '0' + d
  }
  var start_time = y + '-' + m + '-' + d// 开始
  var end_time = y + '-' + m + '-' + d// 开始
  return [start_time, end_time]
}

/**
 * 获取最近七天
 */
export function Forseven() {
  var today = new Date()
  var yesterday = new Date(today.setTime(today.getTime() - 3600 * 1000 * 24 * 7))
  var y = yesterday.getFullYear()
  var m = yesterday.getMonth() + 1
  var d = yesterday.getDate()
  var year = new Date().getFullYear()
  var month = new Date().getMonth() + 1
  var day = new Date().getDate()
  var start_time = y + '-' + m + '-' + d// 开始
  var end_time = year + '-' + month + '-' + day// 结束
  return [start_time, end_time]
}

/**
 * 获取最近一个月
 */
export function Formonth() {
  var today = new Date()
  var yesterday = new Date(today.setTime(today.getTime() - 3600 * 1000 * 24 * 30))
  var y = yesterday.getFullYear()
  var m = yesterday.getMonth() + 1
  var d = yesterday.getDate()
  var year = new Date().getFullYear()
  var month = new Date().getMonth() + 1
  var day = new Date().getDate()
  if (m < 10) {
    m = '0' + m
  }
  if (d < 10) {
    d = '0' + d
  }
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  var start_time = y + '-' + m + '-' + d// 开始
  var end_time = year + '-' + month + '-' + day// 结束
  return [start_time, end_time]
}

export function regFenToYuan(fen) {
  var num = fen
  if (!num) {
    return 0
  }
  num = Math.round(fen) / 100
  return num
}

export default {
  getDay,
  Format,
  Foryest,
  Forseven,
  Formonth,
  regFenToYuan
}
